package com.async.boot.task;

import cn.hutool.core.collection.CollectionUtil;
import com.async.boot.entity.Order;
import com.async.boot.mapper.OrderMapper;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author tanghailan
 * @date 2021年05月15日 22:11
 */
@Component
@Slf4j
public class AsyncTaskTest {

  @Resource
  private OrderMapper orderMapper;

  private AtomicInteger integer = new AtomicInteger(0);

  @Async
  public void execute(List<Order> orderList) {
    if (CollectionUtil.isNotEmpty(orderList)) {
      for (Order item : orderList) {
        try {
          item.setQuantity(20000904);
          log.info("当前线程名称{}", Thread.currentThread().getName());
          orderMapper.updateById(item);
        } catch (Exception e) {
          log.error("统计出错,id=[" + item.getId() + "],error:", e);
        }
      }
    }
//    integer.incrementAndGet();
  }

  public AtomicInteger getInteger() {
    return integer;
  }


  public static void main(String[] args) {
    List<Integer> list = new ArrayList<>();
    for (int i = 1; i <= 80; i++) {
      list.add(i);
    }

    int count = list.size();
    int quantityPerExecution = 5;
    for (int i = 0; i < count; i = i + quantityPerExecution) {
      if (i + quantityPerExecution < count) {
//        System.out.println(i+"_"+(i+9));
        System.out.println(list.subList(i, i + quantityPerExecution));
      } else {
//        System.out.println(i+"_"+(count-1));
        System.out.println(list.subList(i, count));
      }
    }
  }
}
